package com.ljt.yupictureddd.shared.manager.websocket.disruptor;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

/**
 * 图片编辑时间 Disruptor配置
 */
@Configuration
public class PictureEditEventDisruptorConfig {
    @Resource
    @Lazy
    private PictureEditEventWorkHandler pictureEditEventWorkHandler;

    @Bean("pictureEditEventDisruptor")
    public Disruptor<PictureEditEvent> messageModelRingDisruptor() {
        // 定义ringBuffer的大小
        int bufferSize = 1024 * 256;
        // new一个disruptor
        Disruptor<PictureEditEvent> disruptor = new Disruptor<>(PictureEditEvent::new, bufferSize,
                ThreadFactoryBuilder.create()
                        .setNamePrefix("pictureEditEventDisruptor")
                        .build());
        // 设置消费者
        disruptor.handleEventsWithWorkerPool(pictureEditEventWorkHandler);
        // 启动disruptor
        disruptor.start();
        return disruptor;
    }
}
